package com.koi.system.service.post;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koi.common.ex.koi.YUtil;
import com.koi.model.bo.element.SelectLongBo;
import com.koi.model.bo.element.TransferLongBo;
import com.koi.model.vo.system.SysPostVo;
import com.koi.plus.mapper.system.SysPostMapper;
import com.koi.pojo.system.SysPost;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ･ᴗ･
 * @description [岗位信息表-服务实现层]
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements SysPostService {

    private final SysPostMapper sysPostMapper;

    /**
     * @description [分页查询]
     * @author ･ᴗ･
     */
    @Override
    public IPage<SysPost> listPage(SysPostVo vo) {
        // 分页构造器
        Page<SysPost> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        // 进行分页查询
        page = sysPostMapper.listPage(page, vo);
        return page;
    }

    /**
     * @description [查询所有正常岗位]
     * @author ･ᴗ･
     */
    @Override
    public List<TransferLongBo> listLeftPost() {
        return sysPostMapper.listLeftPost();
    }

    /**
     * @description [查询用户拥有正常岗位-穿梭框右侧]
     * @author ･ᴗ･
     */
    @Override
    public List<Long> listRightPost(Long userId) {
        return sysPostMapper.listRightPost(userId);
    }

    /**
     * @description [deleteUserPost]
     * @author ･ᴗ･
     */
    @Override
    public boolean deleteUserPost(Long userId) {
        return sysPostMapper.deleteUserPost(userId);
    }

    /**
     * @description [根据当前用户ID分配岗位-穿梭框]
     * @author ･ᴗ･
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void assignUserPost(Long userId, List<Long> postIds) {
        // 1、删除当前用户拥有的岗位
        this.deleteUserPost(userId);
        if (CollectionUtil.isNotEmpty(postIds) && !postIds.get(0).equals(-1L)) {
            // 2、添加当前用户选中的岗位
            boolean batchAdd = sysPostMapper.batchAddUserPost(userId, postIds);
            YUtil.isTrue(!batchAdd, "分配岗位失败，请重试");
        }
    }

    /**
     * @description [获取岗位下拉框]
     * @author ･ᴗ･
     */
    @Override
    public List<SelectLongBo> listPostElSelect() {
        return sysPostMapper.listPostElSelect();
    }
}
